<?php
/*
* @Author: ecitlm
* @Date:   2017-05-29 19:32:05
* @Last Modified by:   ecitlm
* @Last Modified time: 2017-05-30 18:04:42
*/

namespace app\api\controller;

use phpmailer\phpmailer;
use think\Db;

class Video
{
    function __construct()
    {
        $this->dirroot = $_SERVER['DOCUMENT_ROOT'];
    }

    /**
     * 获取每天是不是节假日
     * 这个接口参数d输入yyyyMMdd格式的日期参数，检查具体日期是否为节假日，工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2；
     */
    public function jiejiari()
    {
        $yyyyMMdd = date('Ymd');
        header('Content-type:text/html;charset=utf-8');
        $url = "https://tool.bitefu.net/jiari/?d=$yyyyMMdd";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $html = curl_exec($ch);
        curl_close($ch);
        $holiday = Db::table('group')->where('group_id', 1)->update(['holiday' => $html]);
        file_put_contents('/www/wwwroot/Doublelow/' . 'holiday.txt', $html);
        file_put_contents('/www/wwwroot/Doublelow/' . 'qiangshu.txt', 'qiangshudatanull');
    }

    /**
     * 视频首页接口
     */
    public function index()
    {
        $isholiday = file_get_contents('/www/wwwroot/Doublelow/' . 'holiday.txt');
        if ($isholiday == 2) {
            echo '节假日休市';
        } elseif ($isholiday == 1) {
            echo '休息日休市';
        } else {
            $morning = strtotime('09:30');
            $morningend = strtotime('11:30');
            $afternoon = strtotime('13:00');
            $afternoonend = strtotime('15:00');
            if (time() > $morning && time() < $morningend) {//上午
                $seconds = rand(1, 60);
                sleep($seconds);
                $this->dowComb();
            } elseif (time() > $afternoon && time() < $afternoonend) {//下午
                $seconds = rand(1, 60);
                sleep($seconds);
                $this->dowComb();
            } elseif (time() > $morningend && time() < $afternoon) {//午间休市
                echo '午间休市';
            } else {
                echo '不在开市时间';
            }
        }
    }

    //少而精策略组合提醒
    public function dowComb()
    {
        header('Content-type:text/html;charset=utf-8');
        $url = 'https://xueqiu.com/p/ZH2921446';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $html = curl_exec($ch);
        curl_close($ch);
        $html = strip_tags($html);
        $html = (explode('SNB.cubeInfo = ', $html));
        $html = (explode('SNB.cubePieData', $html[1]))[0];
        $html = (explode(';', $html))[0];
        $lastdate = json_decode($html, true)['updated_at'];
        $datatime = file_get_contents('/www/wwwroot/Doublelow/' . 'lastdate.txt');
        if (empty($datatime)) {
            file_put_contents('/www/wwwroot/Doublelow/' . 'lastdate.txt', $lastdate);
            echo '数据初始化完成>>>>>>';
        } else {
            if ($datatime < $lastdate) {
                email("前5", "可转债少而精轮动策略更新了", "->我刚刚调整了一下雪球组合 $可转债轮动策略(ZH1332574)$ 的仓位,点击：https://xueqiu.com/P/ZH1332574");
                file_put_contents('/www/wwwroot/Doublelow/' . 'lastdate.txt', $lastdate);
            } else {
                echo '还没有调仓';
            }
        }
    }

    /**
     * 可转债双低策略------本策略不考虑仓库里有新进的强赎，因为策略在大于130的时候，会主动调仓换。触发不了30个交易日的强赎条件
     * @param
     * @btype（E可交债C可转债），dblow（双低值）
     * @return mixed
     * @author yangzhongyi
     */
    public function quadrant()
    {

        system("/www/wwwroot/convertible_bond/shell");
        die;
        $this->doubellow();
        $is_clear = (isset($_GET['is_clear'])) ? intval($_GET ['is_clear']) : 0;
        header('Content-type:text/html;charset=utf-8');
        if ($is_clear == 2) {
            $fucang = file_get_contents('/www/wwwroot/Doublelow/' . 'beifenwarehouse.txt');
            file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', $fucang);
            echo '复仓成功';
            die;
        } elseif ($is_clear == 1) {
            file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', '');
            echo '清仓成功';
            die;
        } else {
            $isholiday = file_get_contents('/www/wwwroot/Doublelow/' . 'holiday.txt');
            if ($isholiday == 2) {
                echo '节假日休市';
            } elseif ($isholiday == 1) {
                echo '休息日休市';
            } else {
                $morning = strtotime('09:33');
                $morningend = strtotime('11:30');
                $afternoon = strtotime('13:00');
                $afternoonend = strtotime('15:00');
                if (time() > $morning && time() < $morningend) {//上午
                    $seconds = rand(1, 60);
                    sleep($seconds);
                    $this->doubellow();
                } elseif (time() > $afternoon && time() < $afternoonend) {//下午
                    $seconds = rand(1, 60);
                    sleep($seconds);
                    $this->doubellow();
                } elseif (time() > $morningend && time() < $afternoon) {//午间休市
                    echo '午间休市';
                } else {
                    echo '不在开市时间';
                }
            }
        }

    }

    //监测可转债双低数据处理0：代码1：名称 3：价格 2：双低值 4：溢价率
    private function doubellow()
    {


        $cookie_jar = tempnam('./tmp', 'cookie');
//        dump($cookie_jar);die;
        header("Content-type:text/html;charset=utf-8");;
        $uri = "https://www.jisilu.cn/account/ajax/login_process/";//这里换成你服务器的地址
        // 参数数组
        $data = [
            '_post_type'     => 'ajax',
            'aes'            => 1,
            'net_auto_login' => '1',
            'password'       => 'zhony1989',
            'return_url'     => 'https://www.jisilu.cn/',
            'user_name'      => 'peofy',
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //提交数据
        //把返回来的cookie信息保存在$cookie_jar文件中
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        //设定返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //设定是否显示头信息
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  //重定向地址也输出
        //设定是否输出页面内容
        curl_setopt($ch, CURLOPT_NOBODY, false);
        $result = curl_exec($ch);
        curl_close($ch);

//        $post_data = [
//            '_post_type'     => 'ajax',
//            'aes'            => 1,
//            'net_auto_login' => '1',
//            'password'       => 'zhony1989',
//            'return_url'     => 'https://www.jisilu.cn/',
//            'user_name'      => 'peofy',
//        ];
//        $url = "https://www.jisilu.cn/account/ajax/login_process";
//        $postdata = http_build_query($post_data);
//        $options = [
//            'http' => [
//                'method'  => 'POST',
//                'header'  => 'Content-type:application/x-www-form-urlencoded',
//                'content' => $postdata,
//                'timeout' => 15 * 60 // 超时时间（单位:s）
//            ]];
//        $context = stream_context_create($options);
//        $result = file_get_contents($url, false, $context);


        dump($result);
        die;


        //get data after login
        header('Content-type:text/html;charset=utf-8');
        list($t1, $t2) = explode(' ', microtime());
        $t = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
        $url = 'https://www.jisilu.cn/data/cbnew/cb_list/?___jsl=LST___t=' . $t;
        $ch2 = curl_init();
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);
        $orders = curl_exec($ch2);
//        echo $orders;
//        exit;
        echo '<pre>';
        echo strip_tags($orders);
        echo '</pre>';
        curl_close($ch2);

        dump($orders);
        die;


        //双低值在哪个范围，定时提醒，目的是想知道是不是一直在这个范围内
        header('Content-type:text/html;charset=utf-8');
        list($t1, $t2) = explode(' ', microtime());
        $t = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
        $url = 'https://www.jisilu.cn/data/cbnew/cb_list/?___jsl=LST___t=' . $t;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $html = curl_exec($ch);
        curl_close($ch);
        $datas = json_decode($html, true)['rows'];

        $finaldata = [];
        $shuangdijunzhi = [];
        $qiangshu = []; //存储已经强生公告的

        foreach ($datas as $key => $value) {//qstatus=03是停牌premium_rt
            if ($value['cell']['redeem_flag'] == 'Y') {
                $qiangshu[] = $value['cell']['bond_id'];
            }

            if ($value['cell']['btype'] == 'E' ||
                $value['cell']['price_tips'] == '待上市' ||
                $value['cell']['year_left'] < 1 ||
                $value['cell']['redeem_flag'] == 'Y' ||
                $value['cell']['qstatus'] == '03'
            ) {
                unset($datas[$key]);
            } else {
                $finaldata[] = $value['cell'];
            }
            //所有的转债用来算所有可转债双低均值大于170就要清仓
            if ($value['cell']['btype'] == 'E' || $value['cell']['price_tips'] == '待上市') {
                unset($datas[$key]);
            } else {
                $dblowmeanarray[] = $value['cell'];
            }
        }

        $dblowmeandata = array_column($dblowmeanarray, 'dblow');//从二维数组里拿出双低值
        //退出条件：双底均值大于170；或者双低值130以下的转债消失。
        if (min($dblowmeandata) > 130) {
            $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail130.txt');//判断是否已发送过邮件
            if ($isemail == 130) {
                echo '双低值130以下的转债消失，停止投资';
            } else {
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail130.txt', 130);
                file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', '');
                email("双底策略", "双低值130以下的转债消失，停止投资", "（清仓了）双低值130以下的转债消失，停止投资点击：https://www.jisilu.cn/data/cbnew/#cb");
            }
        } else {
            $isemail130 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail130.txt');//判断是否已发送过邮件
            if ($isemail130 == 130) {
                email("双底策略", "双低值130以上的转债出现，可以投资", "双低值130以上的转债出现，可以投资点击：https://www.jisilu.cn/data/cbnew/#cb");
            }
            file_put_contents('/www/wwwroot/Doublelow/' . 'isemail130.txt', time());
            $dblowmeanvalue = array_sum($dblowmeandata) / count($dblowmeandata);//算均值
//            $dblowmeanvalue = 1;
            //双低值小于160，仓位30%,双低值小于155，仓位60%；双低值小于150，仓位100%。
            if ($dblowmeanvalue >= 150 && $dblowmeanvalue < 155) {
                $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt');//判断是否已发送过邮件
                if ($isemail == 150) {
                    echo '双低均值大于150小于155';
                    //判断是否是空仓，空仓的话就不要调仓否则会建仓
                    $is_empty = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
                    if (!empty($is_empty)) {
                        $this->tiaocang($finaldata, $qiangshu);
                    }
                } else {
                    echo '双低均值大于150小于155';
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', 150);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', time());
                    email("双底策略", "双低均值大于150小于155", "双低均值大于150小于155,双低值此时小于155，仓位60%,点击：https://xueqiu.com/P/ZH1332574");
                }
            } elseif ($dblowmeanvalue >= 155 && $dblowmeanvalue < 160) {
                $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt');//判断是否已发送过邮件
                if ($isemail == 155) {
                    echo '双低均值大于155小于160';
                    //判断是否是空仓，空仓的话就不要调仓否则会建仓
                    $is_empty = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
                    if (!empty($is_empty)) {
                        $this->tiaocang($finaldata, $qiangshu);
                    }
                } else {
                    echo '双低均值大于155小于160';
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', 155);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', time());
                    email("双底策略", "双低均值大于155小于160", "双低均值大于155小于160，双低值此时小于160，仓位30%,点击：https://xueqiu.com/P/ZH1332574");
                }
            } elseif ($dblowmeanvalue >= 160 && $dblowmeanvalue < 165) {
                $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt');//判断是否已发送过邮件
                if ($isemail == 160) {
                    echo '双低均值大于160小于165';
                    //判断是否是空仓，空仓的话就不要调仓否则会建仓
                    $is_empty = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
                    if (!empty($is_empty)) {
                        $this->tiaocang($finaldata, $qiangshu);
                    }
                    $total170 = file_get_contents('/www/wwwroot/Doublelow/' . 'total160.txt');//讀取出現160的次數
                    if ($total170 == 60) {
                        email("双底策略", "双低均值大于" . $isemail . "小于165持续" . $total170 . "分钟", "双低均值大于160小于165继续减仓,仓位10%，点击：https://xueqiu.com/P/ZH1332574，查看组合是否减仓");
                    }
                    if ($total170 == 120) {
                        email("双底策略", "双低均值大于" . $isemail . "小于165持续" . $total170 . "分钟", "双低均值大于160小于165继续减仓,仓位10%，点击：https://xueqiu.com/P/ZH1332574，查看组合是否减仓");
                    }
                    if ($total170 == 150) {
                        email("双底策略", "双低均值大于" . $isemail . "小于165持续" . $total170 . "分钟", "双低均值大于160小于165继续减仓,仓位10%，点击：https://xueqiu.com/P/ZH1332574，查看组合是否减仓");
                    }
                    if ($total170 == 180) {
                        email("双底策略", "双低均值大于" . $isemail . "小于165持续" . $total170 . "分钟", "双低均值大于160小于165继续减仓,仓位10%，点击：https://xueqiu.com/P/ZH1332574，查看组合是否减仓");
                    }
                    $total170 += 1;
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', $total170);
                } else {
                    echo '双低均值大于160小于165';
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', 160);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', 1);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', time());
                    email("双底策略", "双低均值大于160小于165继续减仓", "双低均值大于160小于165继续减仓,仓位10%，点击：https://xueqiu.com/P/ZH1332574，查看组合是否减仓");
                }
            } elseif ($dblowmeanvalue >= 165 && $dblowmeanvalue < 170) {
                $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt');//判断是否已发送过邮件
                if ($isemail == 165) {
                    echo '双低均值大于165小于170该兑现了';
                    //判断是否是空仓，空仓的话就不要调仓否则会建仓
                    $is_empty = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
                    if (!empty($is_empty)) {
                        $this->tiaocang($finaldata, $qiangshu);
                    }
                    $total170 = file_get_contents('/www/wwwroot/Doublelow/' . 'total165.txt');//讀取出現165的次數
                    if ($total170 == 5) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 10) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 15) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 20) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 25) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 30) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 40) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 50) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 60) {
                        email("双底策略", "双低均值大于" . $isemail . "小于170持续" . $total170 . "分钟", "已经自动清仓");
                        file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', '');
                    }
                    $total170 += 1;
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', $total170);
                } else {
                    echo '双低均值大于165小于170该兑现了';
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', 165);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', 1);
                    email("双底策略", "可转债双低平均值已超过165，现在是兑现的时候。", "点击：https://xueqiu.com/P/ZH1332574，查看组合是否清仓");
                }
            } elseif ($dblowmeanvalue >= 170) {
                $isemail = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt');//判断是否已发送过邮件
                if ($isemail == 170) {
                    echo '双低均值大于170该清倉了';
                    $total170 = file_get_contents('/www/wwwroot/Doublelow/' . 'total170.txt');//讀取出現170的次數
                    if ($total170 == 5) {
                        email("双底策略", "双低均值大于" . $isemail . "持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 10) {
                        email("双底策略", "双低均值大于" . $isemail . "持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 15) {
                        email("双底策略", "双低均值大于" . $isemail . "持续" . $total170 . "分钟", "考虑是否进行手动清仓");
                    }
                    if ($total170 == 20) {
                        email("双底策略", "双低均值大于" . $isemail . "持续" . $total170 . "分钟", "已经自动清仓");
                        file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', '');
                    }
                    $total170 += 1;
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', $total170);
                } else {
                    echo '双低均值大于170该清倉了';
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', 170);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', time());
                    file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', 1);
                    email("双底策略", "可转债双低平均值已超过165，现在是兑现的时候。", "（清仓了）点击：https://xueqiu.com/P/ZH1332574，查看组合是否清仓");
                }
            } else {
                //当双低均值恢复到150以下，跟个邮件提醒
                $isemail150 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt');//判断是否已发送过邮件
                $isemail155 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt');//判断是否已发送过邮件
                $isemail160 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt');//判断是否已发送过邮件
                $isemail165 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt');//判断是否已发送过邮件
                $isemail170 = file_get_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt');//判断是否已发送过邮件
                $isemailarray = [$isemail150, $isemail155, $isemail160, $isemail165, $isemail170];
                //判断是否是空仓，空仓的话就不要发邮件因为会有建仓邮件提醒
                $is_empty = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
                if (!empty($is_empty)) {
                    if (in_array('150', $isemailarray) || in_array('155', $isemailarray) || in_array('160', $isemailarray) || in_array('165', $isemailarray) || in_array('170', $isemailarray)) {
                        email("双底策略", "可转债双低平均值恢复到150以下", "点击：https://xueqiu.com/P/ZH1332574，可转债双低平均值恢复到150以下");
                    }
                }
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail150.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail155.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail160.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail165.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'isemail170.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'total170.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'total165.txt', time());
                file_put_contents('/www/wwwroot/Doublelow/' . 'total160.txt', time());
                $this->tiaocang($finaldata, $qiangshu);
            }
        }

    }

    /**
     * 单独计算调仓，不判断双低均值
     * @param
     * @redeem_flag（string强赎）
     * @return mixed
     * @author yangzhongyi
     */
    public function tiaocang($finaldata, $qiangshu = [])
    {
        $top10 = [];
        $top15 = [];
        $count = count($finaldata);
        $last_names = array_column($finaldata, 'dblow');
        array_multisort($last_names, SORT_ASC, $finaldata);
        //用来获取除了仓库的的转债
        foreach ($finaldata as $kd => $vd) {
            if ((float)$vd['premium_rt'] < 15 && $vd['price'] < 115) {
                $topall[] = ['id' => $vd['bond_id'], 'name' => $vd['bond_nm'], 'shuangdizhi' => $vd['dblow'], 'price' => $vd['price'], 'yijialv' => $vd['premium_rt']];
            }
        }

        $filterData = [];
        //过滤掉溢价率大于15%和价格高于115的
        foreach ($finaldata as $key => $value) {
            if ((float)$value['premium_rt'] < 15 && $value['price'] < 115) {
                $filterData[] = $value;
            }
        }
        //根据债的总数决定仓库内债的个数
        if ($count < 250) {
            foreach ($filterData as $keytop10 => $value10) {
                if ($keytop10 == 10) {
                    break;
                } else {
                    $top10[] = ['id' => $value10['bond_id'], 'name' => $value10['bond_nm'], 'shuangdizhi' => $value10['dblow'], 'price' => $value10['price'], 'yijialv' => $value10['premium_rt']];
                }
            }
            $top = $top10;
        } elseif ($count > 250) {
            foreach ($filterData as $keytop15 => $value15) {
                if ($keytop15 == 15) {
                    break;
                } else {
                    $top15[] = ['id' => $value15['bond_id'], 'name' => $value15['bond_nm'], 'shuangdizhi' => $value15['dblow'], 'price' => $value15['price'], 'yijialv' => $value15['premium_rt']];
                }
            }
            $top = $top15;
        } else {
            email("双底策略", "太TM有意思了，可转债二百五了", "可转债数量正好250个，点击：https://www.jisilu.cn/data/cbnew/#cb");
            foreach ($filterData as $keytop10 => $value10) {
                if ($keytop10 == 10) {
                    break;
                } else {
                    $top10[] = ['id' => $value10['bond_id'], 'name' => $value10['bond_nm'], 'shuangdizhi' => $value10['dblow'], 'price' => $value10['price'], 'yijialv' => $value10['premium_rt']];
                }
            }
            $top = $top10;
        }
        $warehouse = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt');
        $warehouseupdatetime = file_get_contents('/www/wwwroot/Doublelow/' . 'warehouseupdatetime.txt');
        if (empty($warehouse)) {
            file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', json_encode($top));
            file_put_contents('/www/wwwroot/Doublelow/' . 'beifenwarehouse.txt', json_encode($top));
            file_put_contents('/www/wwwroot/Doublelow/' . 'warehouseupdatetime.txt', time());
            $mailtext = '';
            foreach ($top as $kjc => $vjc) {
                $mailtext .= '转债代码：' . $vjc['id'] . '  转债名字：' . $vjc['name'] . '  价格：' . $vjc['price'] . '  双低值：' . $vjc['shuangdizhi'] . PHP_EOL;
            }
            email("双底策略", "建仓", $mailtext);
        } else {

            //单一脉冲调仓：价格125元以上，且需比新标的双低值大10以上，要有一定的阀值，比如双低值129.5轮到119。
            $oldzhuanzhai = [];//仓库的数据
            $newzhuanzhai = [];//最新的数据
            //更新仓库的数据
            $oldzone = $gengxin = $this->object_to_array(json_decode($warehouse));

            foreach ($gengxin as $kgx => $vgx) {
                foreach ($finaldata as $kfna => $vfna) {
                    if ($vgx['id'] == $vfna['bond_id']) {
                        $gengxin[$kgx]['shuangdizhi'] = $vfna['dblow'];
                        $gengxin[$kgx]['price'] = $vfna['price'];
                    }

                }
            }
            $dingqigengxin = $gengxin;

            $oldids = array_column($gengxin, 'id');//仓库的债id
            //找出仓库中没有的新债即新进双低转债
            foreach ($topall as $ktop => $vtop) {
                //根据转债代码判断是否存在于仓库中
                if (!in_array($vtop['id'], $oldids)) {
                    $newzhuanzhai[] = $vtop;
                }
            }
            //复制后面用
            $xinde = $newzhuanzhai;

            //判断仓库里的有没有升到强生公告要强赎
            foreach ($gengxin as $key => $value) {
                if (in_array($value['id'], $qiangshu)) {
                    $maichu[] = $value['id'] . ' ' . $value['name'];//卖出的一维数组
                    $mairu[] = $xinde[0]['id'] . ' ' . $xinde[0]['name'];//买入的一维数组
                    $mailtext = '卖:' . PHP_EOL . implode(PHP_EOL, $maichu) . PHP_EOL . "买:" . PHP_EOL . implode(PHP_EOL, $mairu);
                    email("双底策略", "强赎调仓", "明细：" . PHP_EOL . $mailtext);
                    unset($gengxin[$key]);
                    $gengxin[] = $xinde[0];
                    //二维数组排序后放入仓库
                    $shuangdizhi = array_column($gengxin, 'shuangdizhi');
                    array_multisort($shuangdizhi, SORT_ASC, $gengxin);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', json_encode($gengxin));
                    file_put_contents('/www/wwwroot/Doublelow/' . 'beifenwarehouse.txt', json_encode($gengxin));
                }
            }

            //找出价格大于125的
            foreach ($gengxin as $kware => $vware) {

                if ($vware['price'] > 125) {
                    $oldzhuanzhai[] = $vware;
                    //删除仓库中大于125的
                    unset($gengxin[$kware]);
                }

            }
            //轮动邮件提示
            $lundong = [];//轮动的
            $diff10 = [];//新的和旧的双低值相差10，存新的
            if (count($oldzhuanzhai) > 0) {
                //临时数组根据双低值从大到小排序
                $last_names = array_column($oldzhuanzhai, 'shuangdizhi');
                array_multisort($last_names, SORT_DESC, $oldzhuanzhai);
                //临时数组根据双低值从小到大排序
                $last_names = array_column($newzhuanzhai, 'shuangdizhi');
                array_multisort($last_names, SORT_ASC, $newzhuanzhai);
                //分两种情况：仓库大于125的转债数量小于新进标的和大于新进标的
                foreach ($oldzhuanzhai as $kold => $vold) {//仓库大于125的循环
//                    foreach ($newzhuanzhai as $knew => $vnew) {//当次取到的新债双低
                    if (($vold['shuangdizhi'] - $newzhuanzhai[$kold]['shuangdizhi']) > 10) {//判断两者双低值是否相差10
                        $diff10[] = $newzhuanzhai[$kold];//需要换的写入数组
//                        }
                    }
                }
//                $diff10 = array_unique($diff10, SORT_REGULAR);//去重双低值相差10
                $last_names = array_column($diff10, 'shuangdizhi');
                array_multisort($last_names, SORT_ASC, $diff10);

                if (count($diff10) > 0) {
                    foreach ($oldzhuanzhai as $kold => $vold) {//仓库大于125的循环
                        foreach ($diff10 as $knew => $vnew) {//当次取到的新债双低
                            $lundong[] = [$vold, '换', $vnew];//需要换的写入数组
                            $maichu[] = $vold['id'] . ' ' . $vold['name'];//卖出的一维数组
                            $mairu[] = $vnew['id'] . ' ' . $vnew['name'];//买入的一维数组
                            $gengxin[] = $vnew;//将新的债放入仓库

                        }
                    }
                }

                if (count($lundong) > 0) {
                    //邮件内容
                    $mailtext = '卖:' . PHP_EOL . implode(PHP_EOL, $maichu) . PHP_EOL . "买:" . PHP_EOL . implode(PHP_EOL, $mairu);
                    email("双底策略", "(调仓)单一脉冲调仓", "价格125元以上，且需比新标的双低值大10以上，具体调仓明细：" . PHP_EOL . $mailtext);
                    //二维数组排序后放入仓库
                    $shuangdizhi = array_column($gengxin, 'shuangdizhi');
                    array_multisort($shuangdizhi, SORT_ASC, $gengxin);
                    file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', json_encode($gengxin));
                    file_put_contents('/www/wwwroot/Doublelow/' . 'beifenwarehouse.txt', json_encode($gengxin));
                } else {
                    echo '单一脉冲有价格大于125的没有双低差10的';
                }
            } else {
                echo '单一脉冲没有价格大于125的';
            }
            //定期轮动$dingqigengxin1209600
            $dingqilundong = [];
            if ((time() - 1209600) > $warehouseupdatetime) {
                //仓库更新后的数组根据双低值从大到小排序
                $last_names = array_column($dingqigengxin, 'shuangdizhi');
                array_multisort($last_names, SORT_DESC, $dingqigengxin);
                //新的数组根据双低值从小到大排序
                $last_names = array_column($xinde, 'shuangdizhi');
                array_multisort($last_names, SORT_ASC, $xinde);

                //旧仓库的后面5个换成最新双低前5个
                for ($i = 0; $i < 5; $i++) {
                    $dingqilundong[] = [$dingqigengxin[$i], '换', $xinde[$i]];
                    $maichu1[] = $dingqigengxin[$i]['id'] . ' ' . $dingqigengxin[$i]['name'];//卖出的一维数组
                    $mairu1[] = $xinde[$i]['id'] . ' ' . $xinde[$i]['name'];//买入的一维数组
                    $dingqilundongtemp[] = $xinde[$i];
                    unset($dingqigengxin[$i]);
                }
                //加入仓库
                foreach ($dingqilundongtemp as $klun => $vlun) {
                    $dingqigengxin[] = $vlun;
                }
                //仓库数组重组
                $warehouse = [];
                foreach ($dingqigengxin as $vgengxin) {
                    $warehouse[] = $vgengxin;
                }
                //二维数组排序后放入仓库
                $shuangdizhi = array_column($warehouse, 'shuangdizhi');
                array_multisort($shuangdizhi, SORT_ASC, $warehouse);
                file_put_contents('/www/wwwroot/Doublelow/' . 'warehouse.txt', json_encode($warehouse));
                file_put_contents('/www/wwwroot/Doublelow/' . 'beifenwarehouse.txt', json_encode($warehouse));
                file_put_contents('/www/wwwroot/Doublelow/' . 'warehouseupdatetime.txt', time());
                //邮件内容
                $mailtext1 = '卖:' . PHP_EOL . implode(PHP_EOL, $maichu1) . PHP_EOL . "买:" . PHP_EOL . implode(PHP_EOL, $mairu1);

                $warehousetext = '';
                foreach ($warehouse as $kwarehouse => $vwarehouse) {
                    $warehousetext .= '转债代码：' . $vwarehouse['id'] . '  转债名字：' . $vwarehouse['name'] . '  价格：' . $vwarehouse['price'] . '  双低值：' . $vwarehouse['shuangdizhi'] . PHP_EOL;
                }
                $oldzonetext = '';
                foreach ($oldzone as $koldzone => $voldzone) {
                    $oldzonetext .= '转债代码：' . $voldzone['id'] . '  转债名字：' . $voldzone['name'] . '  价格：' . $voldzone['price'] . '  双低值：' . $voldzone['shuangdizhi'] . PHP_EOL;
                }
                $xindetext = '';
                foreach ($xinde as $kxinde => $vxinde) {
                    $xindetext .= '转债代码：' . $vxinde['id'] . '  转债名字：' . $vxinde['name'] . '  价格：' . $vxinde['price'] . '  双低值：' . $vxinde['shuangdizhi'] . PHP_EOL;
                }
                //发送邮件
                email("双底策略", "（调仓）定期轮动开始了",
                    '（本次操作）换仓明细：' . PHP_EOL . $mailtext1 . PHP_EOL . PHP_EOL . PHP_EOL .
                    '新仓库：' . count($warehouse) . '只' . PHP_EOL . $warehousetext . PHP_EOL . PHP_EOL . PHP_EOL .
                    '旧仓库：' . count($oldzone) . '只' . PHP_EOL . $oldzonetext . PHP_EOL . PHP_EOL . PHP_EOL .
                    '新进标的:' . PHP_EOL . $xindetext
                );
            } else {
                echo '定期轮动周期不到14天';
            }

        }
    }
    /**
     * 强赎回
     * @param
     * @redeem_flag（string强赎）
     * @return mixed
     * @author yangzhongyi
     */
//    public function redeem()
//    {
//        $qiangshu = file_get_contents('/www/wwwroot/Doublelow/' . 'qiangshu.txt');
//        if ($qiangshu == 'qiangshudatanull'){
//            list($t1, $t2) = explode(' ', microtime());
//            $t = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
//            $url = 'https://www.jisilu.cn/data/cbnew/redeem_list/?___jsl=LST___t=' . $t;
//            $ch = curl_init();
//            curl_setopt($ch, CURLOPT_URL, $url);
//            curl_setopt($ch, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
//            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
//            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//            $html = curl_exec($ch);
//            curl_close($ch);
//            $datas = json_decode($html, true)['rows'];
//
//            $redeem = [];
//            foreach ($datas as $key => $value) {
//                if ($value['cell']['redeem_flag'] == 'Y') {
//                    $redeem[] = $value['id'];
//                }
//            }
//        }else{
//            $redeem = $qiangshu;
//        }
//
//        return $redeem;
//    }

    /**
     * tp5邮件
     * @param
     * @return mixed
     * @author staitc7 <static7@qq.com>
     */
    public function email($mailer = "雪球网", $title, $body)
    {

        $toemail = '80837879@qq.com';//定义收件人的邮箱

        $mail = new \PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();// 使用SMTP服务
        $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
        $mail->Host = "smtp.sina.com";// 发送方的SMTP服务器地址
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = "peofy@sina.com";// 发送方的163邮箱用户名，就是你申请163的SMTP服务使用的163邮箱</span><span style="color:#333333;">
        $mail->Password = "406121a5649b696d";// 发送方的邮箱密码，注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码！</span><span style="color:#333333;">
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式</span><span style="color:#333333;">
        $mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994

        $mail->setFrom("peofy@sina.com", "$mailer");// 设置发件人信息，如邮件格式说明中的发件人，这里会显示为Mailer(xxxx@163.com），Mailer是当做名字显示
        $mail->addAddress($toemail, 'Yang');// 设置收件人信息，如邮件格式说明中的收件人，这里会显示为Liang(yyyy@163.com)
        $mail->addReplyTo("peofy@sina.com", "Reply");// 设置回复人信息，指的是收件人收到邮件后，如果要回复，回复邮件将发送到的邮箱地址
        //$mail->addCC("xxx@163.com");// 设置邮件抄送人，可以只写地址，上述的设置也可以只写地址(这个人也能收到邮件)
        //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
        //$mail->addAttachment("bug0.jpg");// 添加附件


        $mail->Subject = date('Y-m-d H:i:s') . $title;// 邮件标题
        $mail->Body = $body;// 邮件正文
        //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容，如果不支持Html方式，就会用到这个，基本无用

        if (!$mail->send()) {// 发送邮件
            echo "Message could not be sent.";
            echo "Mailer Error: " . $mail->ErrorInfo;
            die;// 输出错误信息
        } else {
            echo '发送成功';
        }

    }

    /**
     * @param unknown_type $obj
     * @return unknown
     * @author 脚本之家
     * @date 2013-6-21
     * @todo 将对象转换成数组
     */
    function object_to_array($obj)
    {
        $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
        foreach ($_arr as $key => $val) {
            $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
            $arr[$key] = $val;
        }
        return $arr;
    }
}
